﻿
/*
#**************************************************************************
#  openSIS is a student information system for public and non-public 
#  schools from Open Solutions for Education, Inc. web: www.os4ed.com
#
#  openSIS is  web-based, open source, and comes packed with features that 
#  include student demographic info, scheduling, grade book, attendance, 
#  report cards, eligibility, transcripts, parent portal, 
#  student portal and more.   
#
#  Visit the openSIS web site at http://www.opensis.com to learn more.
#  If you have question regarding this system or the license, please send 
#  an email to info@os4ed.com.
#
#  This program is released under the terms of the GNU General Public License  
#  as  published by the Free Software Foundation, version 2 of the License. 
#  See license.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#**************************************************************************** 

*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OpenSIS.OpenSISWCFServiceReference;
using AjaxControlToolkit;
using System.Globalization;
using OSISToolTip;

namespace OpenSIS.School
{
    public partial class GradeLevel : System.Web.UI.Page
    {
        protected string sCulture;
        protected void Page_Load(object sender, EventArgs e)
        {
            //ddlNextGradeAdd.Style.Add("font-family", "Arial Unicode MS");
            //ddlNextGradeEdit.Style.Add("font-family", "Arial Unicode MS");
            //SchoolBL schoolAdd = new SchoolBL();
            //GradeLevelBL GLBL = new GradeLevelBL();

            TooltipLang TTL = new TooltipLang();
            ModalPopupExtender2.Hide();
            if (Session["MLCulture"] != null && Session["MLUICulture"] != null)
            {
                TTL.tooltip();

                imgbtnAdd.Attributes.Add("onmouseover", "toolTip('"+TTL.Add+"')");
                imgbtnAdd.Attributes.Add("onmouseout", "toolTip()");

            }

            //Session["SchoolID1"] = null;
            // imgbtnEdit.Attributes.Add("onclick", "return GridCheckBoxSelection('" + sCulture + "')");
            try
            {
                if (!Page.IsPostBack)
                {
                    if (Session["MLCulture"] != null && Session["MLUICulture"] != null)
                    {
                        imgbtnEdit.Attributes.Add("onmouseover", "toolTip('" + TTL.Edit + "')");
                        imgbtnEdit.Attributes.Add("onmouseout", "toolTip()");
                        imgbtnDelete.Attributes.Add("onmouseover", "toolTip('" + TTL.Delete + "')");
                        imgbtnDelete.Attributes.Add("onmouseout", "toolTip()");

                    }

                    //if (Session["SchoolID"] != null)
                    //{
                    //    ddlSchoolName.Items.FindByValue(Session["SchoolID"].ToString()).Selected = true;
                    //}
                    //PopulateMasterDropdownFullYear();
                    PopulateGridview();
                    // GLBL.School_Id = Convert.ToInt32(ddlSchoolName.SelectedItem.Value);

                    PopulateNextGrade(ref ddlNextGradeAdd);                  
                
                }
            }
            catch
            {
            }
            finally
            {
                //GLBL = null;
                //schoolAdd = null;
            }
        }

        public void PopulateNextGrade(ref System.Web.UI.WebControls.DropDownList ddl)
        {
            DataSet dsNextGrade = new DataSet();
            OpenSISWCFServiceGradeLevelClient SPClient = new OpenSISWCFServiceGradeLevelClient();                        
            if (Session["SchoolID"] != null)
            {
                dsNextGrade = SPClient.PropulateNextGrade(Convert.ToInt32(Session["SchoolID"]));
            }
           
            SPClient.Close();

            ddl.Items.Clear();
            ddl.DataSource = dsNextGrade.Tables[0];
            ddl.DataTextField = "grd_lvl_short_nm";
            ddl.DataValueField = "grd_lvl_id";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("select", "0"));
        }

        protected void PopulateGridview()
        {
            DataTable dt = new DataTable();

            OpenSISWCFServiceGradeLevelClient SPClient = new OpenSISWCFServiceGradeLevelClient();

           dt = SPClient.PopulateGradeLevel(Convert.ToInt32(Session["SchoolID"]));

           SPClient.Close();
           
         string Culture = System.Web.HttpContext.Current.Session["MLCulture"].ToString();

            if (dt.Rows.Count > 0)
            {

                rlGradeLevel.DataSource = dt;
                rlGradeLevel.DataBind();

                //*******************************6th August//
                //lblmsg.Text = rlGradeLevel.Items.Count.ToString() + " " + "Grade Level were found";


                switch (Culture)
                {
                    case "en-US":
                        lblmsg.Text = rlGradeLevel.Items.Count.ToString() + " " + "Grade Level were found";
                        break;
                    case "es-MX":
                        lblmsg.Text = rlGradeLevel.Items.Count.ToString() + " " + "Grado Nivel se encontraron encontrado";
                        break;
                    case "zh-CN":
                        lblmsg.Text = rlGradeLevel.Items.Count.ToString() + " " + "级级别被发现";
                        break;
                    case "ja-JP":
                        lblmsg.Text = rlGradeLevel.Items.Count.ToString() + " " + "学年レベルが発見された";
                        break;
                    case "de-DE":
                        lblmsg.Text = rlGradeLevel.Items.Count.ToString() + " " + "Grade Level waren gefunden";
                        break;
                    case "ko-KR":
                        lblmsg.Text = rlGradeLevel.Items.Count.ToString() + " " + "학년 수준을 발견했다";
                        break;
                    case "fr-FR":
                        lblmsg.Text = rlGradeLevel.Items.Count.ToString() + " " + "Grade Niveau ont été trouvés";
                        break;
                    case "it-IT":
                        lblmsg.Text = rlGradeLevel.Items.Count.ToString() + " " + "Grado di livello sono stati trovati";
                        break;
                    case "ru-RU":
                        lblmsg.Text = rlGradeLevel.Items.Count.ToString() + " " + "Оценка уровня были найдены";
                        break;
                    case "pt-BR":
                        lblmsg.Text = rlGradeLevel.Items.Count.ToString() + " " + "Grau Nível de  foram encontrados";
                        break;
                    case "ar-SA":
                        lblmsg.Text = rlGradeLevel.Items.Count.ToString() + " " + "تم العثور على الرتبة";
                        break;
                }
                //    //*******************************6th August//


                }
                else
               {


               rlGradeLevel.DataBind();

                //    //*******************************6th August//
                //    //lblmsg.Text = "No Grade Level were found";
               switch (Culture)
               {
                   case "en-US":
                       lblmsg.Text = "No Grade Level were found";
                       break;
                   case "es-MX":
                       lblmsg.Text = "No Grado Nivel eran encontraron";
                       break;
                   case "zh-CN":
                       lblmsg.Text = "没有发现级水平";
                       break;
                   case "ja-JP":
                       lblmsg.Text = "いいえ学年レベルが発見された";
                       break;
                   case "de-DE":
                       lblmsg.Text = "Keine Note Level waren gefunden";
                       break;
                   case "ko-KR":
                       lblmsg.Text = "아니 학년 수준을 발견했다";
                       break;
                   case "fr-FR":
                       lblmsg.Text = "Pas de Grade Niveau ont été trouvés";
                       break;
                   case "it-IT":
                       lblmsg.Text = "No Grado Grado sono stati trovati";
                       break;
                   case "ru-RU":
                       lblmsg.Text = "Оценка уровня не были найдены";
                       break;
                   case "pt-BR":
                       lblmsg.Text = "Não Grau Nível Foram encontrados";
                       break;
                   case "ar-SA":
                       lblmsg.Text = "لا الرتبة تم العثور على";
                       break;
               }
                //    //*******************************6th August//

                }
        }

        //#########################################################################################

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //Function Func = new Function();                
                lblerrmsg.Text = "";
                string Culture = System.Web.HttpContext.Current.Session["MLCulture"].ToString();
                if (txtPreGradeLevelAdd.Text != "" || txtShortNameAdd.Text != "")
                {
                    OpenSISWCFServiceGradeLevelClient SPClient = new OpenSISWCFServiceGradeLevelClient();           

                    string TitleAndShort = SPClient.CheckTitleAndShortName();
                    if (TitleAndShort != "T")
                    {
                        if (TitleAndShort != "S")
                        {
                            SPClient.AddGradeLevel(Convert.ToInt32(Session["SchoolID"]), txtPreGradeLevelAdd.Text.ToString(), txtShortNameAdd.Text.ToString(), Convert.ToInt32(ddlNextGradeAdd.SelectedItem.Value), DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo));
                            //Func.ResetFormControlValues(this.Page);
                            PopulateGridview();
                            PopulateNextGrade(ref ddlNextGradeAdd);

                        }
                        else
                        {

                            //*******************************6th August//
                            lblerrmsg.Text = "Short Name Already Exists";
                            //6thaugust

                            switch (Culture)
                            {
                                case "en-US":
                                    lblerrmsg.Text = "Short Name Already Exists";
                                    break;

                                case "es-MX":
                                    lblerrmsg.Text = "Nombre corto ya existe";
                                    break;
                                case "zh-CN":
                                    lblerrmsg.Text = "短名称已存在";
                                    break;
                                case "ja-JP":
                                    lblerrmsg.Text = "ショート名は既に存在";
                                    break;
                                case "de-DE":
                                    lblerrmsg.Text = "Kurze Namen ist bereits vorhanden";
                                    break;
                                case "ko-KR":
                                    lblerrmsg.Text = "짧은 이름이 이미 존재합니다";
                                    break;
                                case "fr-FR":
                                    lblerrmsg.Text = "Short nom existe déjà";
                                    break;
                                case "it-IT":
                                    lblerrmsg.Text = "Breve nome esiste già";
                                    break;
                                case "ru-RU":
                                    lblerrmsg.Text = "Краткое имя уже существует";
                                    break;
                                case "pt-BR":
                                    lblerrmsg.Text = "Curto nome já existe";
                                    break;
                                case "ar-SA":
                                    lblerrmsg.Text = "الاسم موجود بالفعل قصيرة";
                                    break;
                            }

                            //*******************************6th August//
                            mdlpopwronusernm.Enabled = true;
                            mdlpopwronusernm.Show();
                            return;
                        }
                    }
                    else
                    {
                        //*******************************6th August//
                        lblerrmsg.Text = "Title Already Exists";
                        // return;
                        switch (Culture)
                        {
                            case "en-US":
                                lblerrmsg.Text = "Title Already Exists";
                                break;

                            case "es-MX":
                                lblerrmsg.Text = "Título que ya existe";
                                break;
                            case "zh-CN":
                                lblerrmsg.Text = "标题已经存在";
                                break;
                            case "ja-JP":
                                lblerrmsg.Text = "タイトルが既に存在する";
                                break;
                            case "de-DE":
                                lblerrmsg.Text = "Titel ist bereits vorhanden";
                                break;
                            case "ko-KR":
                                lblerrmsg.Text = "제목이 이미 존재합니다";
                                break;
                            case "fr-FR":
                                lblerrmsg.Text = "Titre existe déjà";
                                break;
                            case "it-IT":
                                lblerrmsg.Text = "Titolo esiste già";
                                break;
                            case "ru-RU":
                                lblerrmsg.Text = "титул уже существует";
                                break;
                            case "pt-BR":
                                lblerrmsg.Text = "Epígrafe já existe";
                                break;
                            case "ar-SA":
                                lblerrmsg.Text = "سند ملكية موجود بالفعل";
                                break;
                        }
                        //*******************************6th August//

                        mdlpopwronusernm.Enabled = true;
                        mdlpopwronusernm.Show();
                        return;
                    }
                }
            }
            catch
            {

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            //Function Func = new Function();
            //try
            //{
            PopulateGridview();
            lblerrmsg.Text = "";
            //    Func.ResetFormControlValues(this.Page);
            //}
            //catch
            //{
            //}
            //finally
            //{
            //    Func = null;
            //}
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            try
            {
                OpenSISWCFServiceGradeLevelClient GLBL = new OpenSISWCFServiceGradeLevelClient();
                GLBL.UpdateGradeLevel(txtShortNameEdit.Text.ToString(),txtTitleEdit.Text.ToString(),Convert.ToInt32(ddlNextGradeEdit.SelectedItem.Value),DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo),Convert.ToInt32(Session["GrdLevelId"]));
                GLBL.Close();

                //ddlSchoolName = (DropDownList)Page.Master.FindControl("ddlSchoolName");
                //GLBL.School_Id = Convert.ToInt32(ddlSchoolName.SelectedItem.Value);
                //dt = GLBL.PopulateGradeLevel();
                //rlGradeLevel.DataSource = dt;
                //rlGradeLevel.DataBind();
                PopulateGridview();
                PopulateNextGrade(ref ddlNextGradeAdd);

            }
            catch
            {

            }
            finally
            {
                ModalPopupExtender2.Enabled = false;
            }

        }

        protected void btnCancelEdit_Click(object sender, EventArgs e)
        {
            ModalPopupExtender2.Enabled = false;
            PopulateGridview();
        }
       
        protected void rlGradeLevel_ItemReorder(object sender, AjaxControlToolkit.ReorderListItemReorderEventArgs e)
        {

            DataTable dt = new DataTable();

            OpenSISWCFServiceGradeLevelClient SPClient = new OpenSISWCFServiceGradeLevelClient();
            try
            {
                dt = SPClient.PopulateGradeLevel(Convert.ToInt32(Session["SchoolID"]));
                SPClient.Close();

                if (dt != null)
                {

                    string strTempOrder = dt.Rows[e.OldIndex]["grd_lvl_cd"].ToString();
                    dt.Rows[e.OldIndex]["grd_lvl_cd"] = dt.Rows[e.NewIndex]["grd_lvl_cd"];
                    dt.Rows[e.NewIndex]["grd_lvl_cd"] = strTempOrder;
                    dt.AcceptChanges();


                    int GrdLevelId = Convert.ToInt32(dt.Rows[e.NewIndex]["grd_lvl_id"].ToString());
                    string GradeLevelShortOrder = dt.Rows[e.NewIndex]["grd_lvl_cd"].ToString();
                    
                    OpenSISWCFServiceGradeLevelClient SPGrdClient = new OpenSISWCFServiceGradeLevelClient();
                    SPGrdClient.UpdateGradeShortOrder(GradeLevelShortOrder,GrdLevelId);
                    SPGrdClient.Close();

                    int GrdLevelIdOld = Convert.ToInt32(dt.Rows[e.OldIndex]["grd_lvl_id"].ToString());
                    string GradeLevelShortOrderOld = dt.Rows[e.OldIndex]["grd_lvl_cd"].ToString();
                    OpenSISWCFServiceGradeLevelClient SPGrdOldClient = new OpenSISWCFServiceGradeLevelClient();
                    SPGrdOldClient.UpdateGradeShortOrder(GradeLevelShortOrderOld, GrdLevelIdOld);
                    SPGrdOldClient.Close();

                    OpenSISWCFServiceGradeLevelClient SPCClient = new OpenSISWCFServiceGradeLevelClient();
                    rlGradeLevel.DataSource = SPCClient.PopulateGradeLevel(Convert.ToInt32(Session["SchoolID"])); ;
                    rlGradeLevel.DataBind();

                }
            }
            catch
            {
            }
            finally
            {

            }

        }

        protected void rlGradeLevel_ItemDataBound(object sender, ReorderListItemEventArgs e)
        {
            try
            {
               
                if (e.Item.ItemType == ListItemType.Item)
                {
                    DropDownList ddlNextGradeAdd = (DropDownList)e.Item.FindControl("ddlNextGrade");
                    Label lblNextGrade = (Label)e.Item.FindControl("lblNextGrade");
                    Label lblTitle = (Label)e.Item.FindControl("lblTitle");
                    Label lblShortName = (Label)e.Item.FindControl("lblShortName");
                    HiddenField hndNextGradeId = (HiddenField)e.Item.FindControl("hndNextGradeId");

                    if (ddlNextGradeAdd != null)
                    {
                        PopulateNextGrade(ref ddlNextGradeAdd);
                        if (hndNextGradeId.Value.ToString() != "")
                        {
                            ddlNextGradeAdd.Items.FindByValue(hndNextGradeId.Value.ToString()).Selected = true;
                        }
                    }
                }                
            }
            catch
            {

            }
        }

        protected void imgbtnEdit_Click(object sender, ImageClickEventArgs e)
        {
            
            try
            {
                foreach (ReorderListItem RI in rlGradeLevel.Items)
                {

                    if (RI.ItemType == ListItemType.Item || RI.ItemType == ListItemType.AlternatingItem)
                    {

                        CheckBox chk = (CheckBox)RI.FindControl("chkrow");
                        if (chk.Checked)
                        {

                            HiddenField hdnGrdLevelId = (HiddenField)RI.FindControl("hdnGrdLevelId");
                            Label lblTitle = (Label)RI.FindControl("lblTitle");
                            Label lblShortName = (Label)RI.FindControl("lblShortName");
                            Label lblNextGrade = (Label)RI.FindControl("lblNextGrade");
                            HiddenField hndNextGradeId = (HiddenField)RI.FindControl("hndNextGradeId");

                            txtTitleEdit.Text = lblTitle.Text.ToString();
                            txtShortNameEdit.Text = lblShortName.Text.ToString();

                            if (ddlNextGradeEdit != null)
                            {
                                //GLBL.School_Id = Convert.ToInt32(ddlSchoolName.SelectedItem.Value);
                                PopulateNextGrade(ref ddlNextGradeEdit);
                                if (hndNextGradeId.Value.ToString() != "")
                                {
                                    ddlNextGradeEdit.Items.FindByValue(hndNextGradeId.Value.ToString()).Selected = true;
                                }
                            }

                            Session["GrdLevelId"] = hdnGrdLevelId.Value.ToString();


                        }

                    }
                }


            }
            catch
            {

            }

            ModalPopupExtender2.Enabled = true;
            ModalPopupExtender2.Show();
        }

        protected void imgbtnDelete_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string Culture = System.Web.HttpContext.Current.Session["MLCulture"].ToString();
                lblerrmsg.Text = "";
                lblerrmsg.Text = "";
                foreach (ReorderListItem RI in rlGradeLevel.Items)
                {

                    if (RI.ItemType == ListItemType.Item || RI.ItemType == ListItemType.AlternatingItem)
                    {

                        CheckBox chk = (CheckBox)RI.FindControl("chkrow");
                        HiddenField hdnGrdLevelId = (HiddenField)RI.FindControl("hdnGrdLevelId");
                        if (chk.Checked)
                        {
                            OpenSISWCFServiceGradeLevelClient GLBL = new OpenSISWCFServiceGradeLevelClient();

                            DataTable dt = GLBL.GradeLevelDeleteCheck(Convert.ToInt32(hdnGrdLevelId.Value));
                            if (Convert.ToInt32(dt.Rows[0][0]) < 1)
                            {
                                DataTable dtNGL = GLBL.GradeLevelUseInNextGradeLevelCheck(Convert.ToInt32(hdnGrdLevelId.Value));
                                if (Convert.ToInt32(dtNGL.Rows[0][0]) < 1)
                                {                                    
                                    GLBL.DeleteGradeLevel(Convert.ToInt32(hdnGrdLevelId.Value));
                                    GLBL.Close();
                                    PopulateNextGrade(ref ddlNextGradeAdd);
                                }
                                else
                                {
                                    lblerrmsg.Text = "Grade Level is in Used";
                                    GLBL.Close();
                                    switch (Culture)
                                    {

                                        case "en-US":
                                            lblerrmsg.Text = "Grade Level is in Use";
                                            break;

                                        case "es-MX":
                                            lblerrmsg.Text = "Grado Nivel encuentra en uso";
                                            break;
                                        case "zh-CN":
                                            lblerrmsg.Text = "级别 正在使用中";
                                            break;
                                        case "ja-JP":
                                            lblerrmsg.Text = "学年 レベル 使用 されています ";
                                            break;
                                        case "de-DE":
                                            lblerrmsg.Text = "Grade ist im Einsatz";
                                            break;
                                        case "ko-KR":
                                            lblerrmsg.Text = "학년 수준  이다 안에 사용";
                                            break;
                                        case "fr-FR":
                                            lblerrmsg.Text = "Grade Niveau est en cours d'utilisation";
                                            break;
                                        case "it-IT":
                                            lblerrmsg.Text = "Grado è in uso";
                                            break;
                                        case "ru-RU":
                                            lblerrmsg.Text = "Оценка уровня в использования";
                                            break;
                                        case "pt-BR":
                                            lblerrmsg.Text = "Votação nível está em uso";
                                            break;
                                        case "ar-SA":
                                            lblerrmsg.Text = "منزلة المستوى يكون في الاستخد   ";
                                            break;
                                    }



                                    mdlpopwronusernm.Enabled = true;
                                    mdlpopwronusernm.Show();
                                }
                            }
                            else
                            {
                                lblerrmsg.Text = "Grade Level is in use";
                                GLBL.Close();

                                switch (Culture)
                                {

                                    case "en-US":
                                        lblerrmsg.Text = "Grade Level is in Use";
                                        break;

                                    case "es-MX":
                                        lblerrmsg.Text = "Grado Nivel encuentra en uso";
                                        break;
                                    case "zh-CN":
                                        lblerrmsg.Text = "级别 正在使用中";
                                        break;
                                    case "ja-JP":
                                        lblerrmsg.Text = "学年 レベル 使用 されています ";
                                        break;
                                    case "de-DE":
                                        lblerrmsg.Text = "Grade ist im Einsatz";
                                        break;
                                    case "ko-KR":
                                        lblerrmsg.Text = "학년 수준  이다 안에 사용";
                                        break;
                                    case "fr-FR":
                                        lblerrmsg.Text = "Grade Niveau est en cours d'utilisation";
                                        break;
                                    case "it-IT":
                                        lblerrmsg.Text = "Grado è in uso";
                                        break;
                                    case "ru-RU":
                                        lblerrmsg.Text = "Оценка уровня в использования";
                                        break;
                                    case "pt-BR":
                                        lblerrmsg.Text = "Votação nível está em uso";
                                        break;
                                    case "ar-SA":
                                        lblerrmsg.Text = "منزلة المستوى يكون في الاستخد   ";
                                        break;
                                }




                                mdlpopwronusernm.Enabled = true;
                                mdlpopwronusernm.Show();
                            }
                        }
                    }


                }
                PopulateGridview();
            }
            catch
            {

            }
        }

        private void ml_InitCulture()
        {
            if (Session["MLCulture"] != null && Session["MLUICulture"] != null)
            {
                String MLCultureName = (String)Session["MLCulture"];
                sCulture = Session["MLCulture"].ToString();
                String MLUICultureName = (String)Session["MLUICulture"];

                if ((MLCultureName != null) && (MLCultureName.Length > 0))
                {
                    Page.Culture = MLCultureName;
                }

                if ((MLUICultureName != null) && (MLUICultureName.Length > 0))
                {
                    Page.UICulture = MLUICultureName;
                }
            }
        }



        protected override void InitializeCulture()
        {
            ml_InitCulture();
        }




    }
}